#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 3e5 + 10;

struct node { ll val; int x; } v[N];
ll adt[N];
int ch[N][2], ndn, n, m, ld[N], dep[N], rt[N];
ll f[N];
vec G[N];
veg pot[N];

void pt(int x, ll w) { if(!x) return; v[x].val += w; adt[x] += w; }
void pd(int x) { if(adt[x]) pt(ch[x][0], adt[x]), pt(ch[x][1], adt[x]), adt[x] = 0; }
int merge(int x, int y) {
	if(!x || !y) return x | y;
	if(v[x].val > v[y].val) swap(x, y); pd(x); ch[x][1] = merge(ch[x][1], y); if(ld[ch[x][0]] < ld[ch[x][1]]) swap(ch[x][0], ch[x][1]);
	ld[x] = ld[ch[x][0]] + 1; return x;
}

void dfs(int x, int p) {
	dep[x] = dep[p] + 1;
	for(auto v : pot[x]) ++ndn, ::v[ndn].val = v.se, ::v[ndn].x = v.fi, rt[x] = merge(rt[x], ndn);
	ll tot = 0;
	for(auto y : G[x]) if(y ^ p) dfs(y, x), tot += f[y], pt(rt[y], -f[y]), rt[x] = merge(rt[x], rt[y]);
	pt(rt[x], tot); while(rt[x] && dep[v[rt[x]].x] >= dep[x]) pd(rt[x]), rt[x] = merge(ch[rt[x]][0], ch[rt[x]][1]);
	if(x != 1 && !rt[x]) puts("-1"), exit(0); f[x] = v[rt[x]].val;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in; rep(i, 2, n) { int u = in, v = in; G[u].eb(v); G[v].eb(u); }
	rep(i, 1, m) { int u = in, v = in, w = in; pot[u].eb(v, w); }
	dfs(1, 0); ll ans = 0; for(auto y : G[1]) ans += f[y]; printf("%lld\n", ans);
	return 0;
}
